﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
public class CatchThatException
{
    public static int LogErrorTxt(Exception exSend, String logFile)
    {
        try
        {
            StreamWriter objWriter = new StreamWriter(logFile.ToString(), true);
            objWriter.WriteLine("Exception accoured at:");
            objWriter.WriteLine();
            objWriter.WriteLine(DateTime.Now.ToString());
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("Exception Message:");
            objWriter.WriteLine();
            objWriter.WriteLine(exSend.Message.ToString());
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("Exception Source");
            objWriter.WriteLine();
            objWriter.WriteLine(exSend.Source.ToString());
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("Exception Stack Trace");
            objWriter.WriteLine();
            objWriter.WriteLine(exSend.StackTrace.ToString());
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("Help Link");
            objWriter.WriteLine();
            if (exSend.HelpLink == null)
            {
                objWriter.WriteLine("No help link available");
            }
            else
            { objWriter.WriteLine(exSend.HelpLink.ToString()); };
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("InnerException");
            objWriter.WriteLine();
            if (exSend.InnerException == null)
            {
                objWriter.WriteLine("No InnerException available");
            }
            else
            {
                objWriter.WriteLine(exSend.InnerException.ToString());
            }
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("Target Site");
            objWriter.WriteLine();
            objWriter.WriteLine(exSend.TargetSite.ToString());
            objWriter.WriteLine();
            objWriter.WriteLine("====================================");
            objWriter.WriteLine("====================================");
            objWriter.Dispose();
            objWriter.Close();
            return 0;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static int LogErrorMSAccess(Exception exSend,string pathMDB)
    {
        string hlink = "";
        if (exSend.HelpLink == null)
        {
            hlink = "No help link available";
        }
        else
        {
            hlink = exSend.HelpLink;
        }
        string IException = "";
        if (exSend.InnerException == null)
        {
            IException = "No InnerException available";
        }
        else
        {
            IException = exSend.InnerException.ToString();
        }
        try
        {
            newlog(DateTime.Now.ToString(), exSend.Message.ToString(),
                exSend.StackTrace.ToString(), exSend.Source.ToString(),
                hlink, IException, exSend.TargetSite.ToString(),pathMDB);
        }
        catch (Exception ex)
        {
            throw ex;
            
        }
        return 0;
    }

    public static int newlog(string dateLog, string exceptionMessage, string stackTrace, string exceptionSource, string helpLink, string innerException, string targetSite,string pMDB)
    {
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source="+pMDB;
        System.Data.IDbConnection dbConnection = new System.Data.OleDb.OleDbConnection(connectionString);

        string queryString = "INSERT INTO [Log] ([DateLog], [ExceptionMessage], [StackTrace], [ExceptionSource]" +
            ", [HelpLink], [InnerException], [TargetSite]) VALUES (@DateLog, @ExceptionMessag" +
            "e, @StackTrace, @ExceptionSource, @HelpLink, @InnerException, @TargetSite)";
        System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
        dbCommand.CommandText = queryString;
        dbCommand.Connection = dbConnection;

        System.Data.IDataParameter dbParam_dateLog = new System.Data.OleDb.OleDbParameter();
        dbParam_dateLog.ParameterName = "@DateLog";
        dbParam_dateLog.Value = dateLog;
        dbParam_dateLog.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_dateLog);
        System.Data.IDataParameter dbParam_exceptionMessage = new System.Data.OleDb.OleDbParameter();
        dbParam_exceptionMessage.ParameterName = "@ExceptionMessage";
        dbParam_exceptionMessage.Value = exceptionMessage;
        dbParam_exceptionMessage.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_exceptionMessage);
        System.Data.IDataParameter dbParam_stackTrace = new System.Data.OleDb.OleDbParameter();
        dbParam_stackTrace.ParameterName = "@StackTrace";
        dbParam_stackTrace.Value = stackTrace;
        dbParam_stackTrace.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_stackTrace);
        System.Data.IDataParameter dbParam_exceptionSource = new System.Data.OleDb.OleDbParameter();
        dbParam_exceptionSource.ParameterName = "@ExceptionSource";
        dbParam_exceptionSource.Value = exceptionSource;
        dbParam_exceptionSource.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_exceptionSource);
        System.Data.IDataParameter dbParam_helpLink = new System.Data.OleDb.OleDbParameter();
        dbParam_helpLink.ParameterName = "@HelpLink";
        dbParam_helpLink.Value = helpLink;
        dbParam_helpLink.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_helpLink);
        System.Data.IDataParameter dbParam_innerException = new System.Data.OleDb.OleDbParameter();
        dbParam_innerException.ParameterName = "@InnerException";
        dbParam_innerException.Value = innerException;
        dbParam_innerException.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_innerException);
        System.Data.IDataParameter dbParam_targetSite = new System.Data.OleDb.OleDbParameter();
        dbParam_targetSite.ParameterName = "@TargetSite";
        dbParam_targetSite.Value = targetSite;
        dbParam_targetSite.DbType = System.Data.DbType.String;
        dbCommand.Parameters.Add(dbParam_targetSite);

        int rowsAffected = 0;
        dbConnection.Open();
        try
        {
            rowsAffected = dbCommand.ExecuteNonQuery();
        }
        finally
        {
            dbConnection.Close();
        }

        return rowsAffected;
    }

}

